<template>
  <div>
    <el-dialog
      title="提示"
      :visible.sync="roleDilalogShow"
      width="50%"
      @click="btnColse"
    >
      <el-checkbox-group v-model="roleIds">
        <el-checkbox v-for="(item,i) in list" :key="i" :label="item.id">
          {{ item.name }}
        </el-checkbox>
      </el-checkbox-group>
      <el-row slot="footer" class="dialog-footer" type="flex" justify="center" align="middle">
        <el-button @click="btnColse">取 消</el-button>
        <el-button type="primary" @click="btnOk">确 定</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import { assignRoles, getRoleListAPI } from '@/api/setting'
import { getUserDetailByIdAPI } from '@/api/employee'
export default {
  name: 'AsignRole',
  components: {},
  props: {
    userId: {
      type: [Number, String],
      required: true,
      default: ''
    }
  },
  data() {
    return {
      roleDilalogShow: false,
      list: [], // 所有角色的id
      roleIds: [] // 当前用户拥有的id
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getRoleList()
  },
  methods: {
    // 获得所有权限列表
    async  getRoleList() {
      const { rows } = await getRoleListAPI({ page: 1, pagesize: 100 })
      this.list = rows
    },
    // 根据id 获得权限详情
    async getUserDetailById(id) {
      const { roleIds } = await getUserDetailByIdAPI(id)
      this.roleIds = roleIds
    },
    btnColse() {
      this.roleDilalogShow = false
    },
    async btnOk() {
      await assignRoles({
        id: this.userId,
        roleIds: this.roleIds
      })
      this.$message.success('保存成功')
      this.btnColse()
    }
  }
}
</script>

<style lang="less" scoped>
</style>
